<?php
/*
	get stats functions
*/

class stats extends seo_engine
{
	public function get_global_rank( $domain_root )
	{
		$str = $this->doRequest( 'http://www.alexa.com/siteinfo/'.$domain_root );

		if( preg_match_all('#title=\'Global rank icon\' alt=\'Global rank icon\'><strong class="metrics-data align-vmiddle">(.*?)</strong>#s', $str, $global_rank))
			$stats['Global_rank'] =  $global_rank[1];	
		else
			echo "Page not found!";

		return $stats;
	}

	public function get_robots( $domain_root )
	{
		$stats['Disallow'] = $this->doRequest($domain_root."/robots.txt");
		
		if( preg_match_all('#Disallow: (.*?)\n#s', $stats['Disallow'], $findDisallow) )
		{
			return $findDisallow[1];
		}
		else
		{
			$stats['Disallow'] = "Robots.tx missing";
		}

		return $stats;
	}

	public function get_results( $domain_root )
	{
		
		$str1 = $this->doRequest("https://www.google.ro/?#q=site:".$domain_root);
	
		if( preg_match('#<div id="resultStats">(.*?)<nobr>(.*?)</nobr></div>#', $str1, $G_stats))
			$stats['G_Stats'] = $G_stats[1];
		else
			$stats['G_Stats'] = "Results not found";
			
		$str2 = $this->doRequest("https://search.yahoo.com/search;_ylt=_?p=site%3A".$domain_root);

		if( preg_match('#<div id="pg" data-bns="Yahoo" data-bk="(.*?)"><span>(.*?)</span></div>#', $str2, $Y_stats)) 
			$stats['Y_Stats'] = $Y_stats[2];
		
		else if( preg_match('#Next</a><span>(.*?)</span>#', $str2, $Y_stats))
			$stats['Y_Stats'] = $Y_stats[1];				
		
		else
			$stats['Y_Stats'] = "Results not found";

		$str3 = $this->doRequest("http://www.bing.com/search?q=site%3A".$domain_root);

		if( preg_match('#<span class="sb_count" id="count">(.*?)</span>#', $str3, $B_stats))
			$stats['B_Stats'] = $B_stats[1];
		else
			$stats['B_Stats'] = "Results not found";

		return $stats;
	}

	public function get_stats ( $domain_root )
	{
		$stats['stats']     		= $this->get_results($domain_root);
		$stats['robots']    		= $this->get_robots($domain_root);
		$stats['Global_rank']		= $this->get_global_rank($domain_root);

		return $stats;
	}

	public function get_template( $action = false )
	{
		// safety, prevent other cases
		if(!is_numeric($action))
			$action = 0;


		switch($action)
		{
			case 0:
				$t = "<form method='GET'>
					<input type='hidden' name='page' value='".get_class($this)."' />
					<input type='hidden' name='action' value='1' />
					<input type='text' name='url' value='http://'>
					<input type='submit' value='Submit'/>
				</form>";
			break;

			case 1:
				//global $seo;
				// make sure that url was submitted
				if(!isset($_GET['url']))
				{
					$t = "Error, no domain was received";
					break;
				}
				
				$url = $_GET['url'];
				if( !$this->trimt_to_root($url) )
				{ 	
					$t = "Error, not a valid url inserted!";
					break;
				}

				// save domain root
				$domain_root 	= $this->trimt_to_root($url);
				$stats 			= $this->get_stats($domain_root);
					
				var_dump($stats);			

				$t = "<table border=1px><tr>
					  <th>Google search results</th>
					  <th>Yahoo search results</th>
					  <th>Bing search results</th>
					  <th>Robots.txt</th>
					  <th>Global rank</th></tr>
					  <tr><td>".$stats['stats']['G_Stats']."</td> 
					  <td>".$stats['stats']['Y_Stats']."</td>
					  <td>".$stats['stats']['B_Stats']."</td>
					  <td>".$stats['Global_rank']."</td>
					  </tr></table>

					  <h2>Robots.txt</h2>
					  <div>
					  ";
				foreach($stats['robots'] as $dissalow)
				{
					$t.="<a href='#'>".$domain_root.$dissalow."</a><br/>";
				}

			break;

			default:
				$t = "Page doesn't exist";
		}

		return $t;
	}
}
?>